package main

import (
	"fmt"
	"gitee.com/alex-pistachio/axel/logs"
	"gitee.com/alex-pistachio/axel/utils"
	"gitee.com/alex-pistachio/axel/web"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/sirupsen/logrus"
	"time"
)

var logger = logs.GetLoggerWithPackageName()

type HelloController struct {
	web.Controller

	Name string `json:"name"`
	Age  int    `json:"age"`
	Id   string `json:"id"`
}

func (h *HelloController) Execute() {
	h.Id = utils.NanoId()
	logger.WithFields(logrus.Fields{
		"name":   h.Name,
		"age":    h.Age,
		"nanoid": h.Id,
	}).Info("parameters...")

	h.Resp().Data = h
}

func init() {
	logs.UseConsoleAppender(logrus.DebugLevel, "json")
}

func main() {
	logs.Logger().WithField("ts", time.Now().UnixNano()).Debug("debug level")
	logs.SetLogConfig(logrus.InfoLevel, "text")
	logs.Logger().WithField("ts", time.Now().UnixNano()).Debug("debug level")
	logs.Logger().WithField("ts", time.Now().UnixNano()).Info("debug level")
	logs.SetLogConfig(logrus.InfoLevel, "text")
	logs.Logger().WithField("ts", time.Now().UnixNano()).Info("debug level")

	h := server.New(server.WithHostPorts(":9000"))
	//h.Use(web.NewRequestIdWithUUID(), web.RestAccessLog())
	h.Use(web.NewRequestIdWithNanoid(), web.RestAccessLog())

	for i := 0; i < 10; i++ {
		fmt.Println(utils.NanoId())
	}

	r := h.Group("/api")

	r.POST("/hi", web.NewRestHandler(&HelloController{}))

	h.Spin()
}
